function p_idat = fsb_pad_idat(c_idat,disp,t)

% FSB : take compacted idats and pad their borders
%
% EXAMPLE:
% p_idat = fsb_pad_idat(c_idat,1,[48 48 12])
%
% INPUT:
% c_ idat:          4-D image data
% disp:             set this to one to display graphic output
% t:                New image size (e.g. [48 48 40])
% sandbox:          sandbox struct
%
% OUTPUT:
% p_idat:           A padded 4D- Volume
%
% CALLED BY:
% FSB.m
% fsb_load_files.m
%
% NOTES:
% this function is supposed to use compacted idats and pad their borders to
% make it easier to combine different data sets.
% it assumes that there are not too many possibilites for dataset sizes
% feel free to add more dataset sizes according to your own needs
%
% Copyright 2010 MPI for Biological Cybernetics
% Author: Steffen Stoewer
% License:GNU GPL, no express or implied warranties
%
% $Revision 1.0
%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if nargin<2
    disp = 0
end
[a b c d] = size (c_idat);
cent = [round(a/2) round(b/2) round(c/2)];
cent2 = [(a/2) (b/2) (c/2)];
cent_diff = round(cent-cent2);

if exist('t','var')==0;
    %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    % Set up an array of commonly used resolutions
    %~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    t = [
        48 48 20
        60 46 20
        64 48 20
        64 64 20
        64    64    40
        96    64    40
        96    96    60
        128   128    32
        128   128    40
        128   128    60
        128   128   128
        140 190 140
        128   128   160
        256   256   100
        256   256   110
        256   256   120
        256   256   128
        256   256   160
        384 384 120
        384 384 160
        512   512   160
        512   512   240
        ];
end

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% Find the smallest predefined format that could be used
%~~~~~~~~~~~~~~~~~~~~~~~~~~~
for y=1:size(t,1);
    if t(y,1)>=a && t(y,2)>=b && t(y,3)>=c;
        an = t(y,1);bn = t(y,2); cn = t(y,3);
        pa = floor((an-a)/2); pb = floor((bn-b)/2); pc = floor((cn-c)/2);
        %p_idat = int16(padarray(c_idat,[pa pb pc],0,'both'));
        p_idat = padarray(c_idat,[pa pb pc],0,'both');
        p_idat = (padarray(p_idat,cent_diff,0,'post'));
        if disp == 1;
            fsb_display_brains(c_idat,p_idat)
        end

        return;
    end
end




